<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class='box-card'>
      <!-- 添加分类的按钮 -->
      <el-row>
        <el-col>
          <el-button type='primary' @click='showAddDialog'>添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 商品分类列表 树形表格依赖 第三方依赖 -->
      <tree-table class='tree-table' :data="cateList" index-text='#' :expand-type='false' :selection-type='false'
        show-index :stripe="props.stripe" :border="props.border" ref="treeTableRef" :columns='columns'>
        <!-- 是否有效 -->
        <template slot='isok' slot-scope='scope'>
          <i style='color: lightgreen;' v-if='scope.row.cat_deleted === false' class="el-icon-circle-check"></i>
          <i style='color: red;' v-else class="el-icon-circle-close"></i>
        </template>
        <!-- 排序 -->
        <template slot='order' slot-scope='scope'>
          <el-tag v-if='scope.row.cat_level === 0' size='mini'>一级</el-tag>
          <el-tag v-else-if='scope.row.cat_level === 1' size='mini' type='success'>二级</el-tag>
          <el-tag v-else size='mini' type='warning'>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot='opt'>
          <el-button size='mini' type="primary" icon="el-icon-edit"></el-button>
          <el-button size='mini' type="danger" icon="el-icon-delete"></el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination :current-page="queryInfo.pagenum" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :page-sizes="[1, 5, 10, 15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框-->
    <el-dialog @closed='addFormDialogClosed' title="添加分类" :visible.sync="showAddDialogVisiable" width="50%">
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <!-- 级联选择器 -->
        <el-form-item label="父级分类：">
          <!-- options 用来指定数据源 -->
          <!-- props 用来指定配置对象 -->
          <el-cascader show-all-levels :options="parentCateList" :props="cascaderProps" v-model="selectedKeys"
            @change="parentCateChanged" clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddDialogVisiable = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        // 查询信息
        queryInfo: {
          type: 3,
          pagesize: 5,
          pagenum: 1
        },
        // 商品分类
        cateList: [],
        // 商品数量
        total: 0,
        columns: [{
            label: '分类名称',
            prop: 'cat_name',
            width: '300px'
          },
          {
            label: '是否有效',
            prop: 'cat_deleted',
            width: '300px',
            type: 'template',
            template: 'isok'
          },
          {
            label: '排序',
            width: '300px',
            type: 'template',
            template: 'order'
          },
          {
            label: '操作',
            width: '300px',
            type: 'template',
            template: 'opt'
          }
        ],
        props: {
          stripe: false,
          border: true
        },
        // 添加分类对话框的显示与隐藏
        showAddDialogVisiable: false,
        // 添加分类表单数据
        addCateForm: {
          cat_name: '',
          cat_pid: ''
        },
        // 表单验证规则
        addCateFormRules: {
          cat_name: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }]
        },
        // 父级分类数据
        parentCateList: [],
        cascaderProps: {
          checkStrictly: true,
          expandTrigger: 'hover',
          value: 'cat_id',
          label: 'cat_name',
          children: 'children'
        },
        selectedKeys: []
      }
    },
    created() {
      this.getCateList()
    },
    methods: {
      // 获取商品分类数据
      async getCateList() {
        const {
          data: res
        } = await this.$http.get('categories', {
          params: this.queryInfo
        })
        if (res.meta.status !== 200) {
          return this.$message.error('获取商品分类数据失败')
        }
        // console.log(res.data)
        // 保存获取到的分类数据到data中
        this.cateList = res.data.result
        this.total = res.data.total
      },
      // 分页数据改变时触发
      handleSizeChange(newSize) {
        this.queryInfo.pagesize = newSize
        this.getCateList()
      },
      // 当前页码改变
      handleCurrentChange(newPage) {
        this.queryInfo.pagenum = newPage
        this.getCateList()
      },
      // 点击添加分类按钮
      showAddDialog() {
        // 获取到所有的父级分类数据
        this.getParentCateList()
        // 显示对话框
        this.showAddDialogVisiable = true
      },
      // 获取父级分类的数据
      async getParentCateList() {
        const {
          data: res
        } = await this.$http.get('categories', {
          params: {
            type: 2
          }
        })
        // console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('获取父级分类数据失败')
        }
        console.log(res.data)
        this.parentCateList = res.data
      },
      // 当选中项改变
      parentCateChanged() {
        console.log(this.selectedKeys)
        // 判断是否有选中父级分类项
        if (this.selectedKeys.length > 0) {
          this.addCateForm.cat_id = this.selectedKeys[this.selectedKeys.length - 1]
          this.addCateForm.cat_level = this.selectedKeys.length
        } else {
          this.addCateForm.cat_id = 0
          this.addCateForm.cat_level = 0
        }
      },
      // 确认提交添加分类
      addCate() {
        // 表单预验证判断
        this.$refs.addCateFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$http.post('categories', {
            cat_pid: this.addCateForm.cat_id,
            cat_name: this.addCateForm.cat_name,
            cat_level: this.addCateForm.cat_level
          })
          if (res.meta.status !== 201) return this.$message.error('添加分类失败')
          // 添加分类成功提示
          this.$message.success('添加分类成功')
          // 刷新商品列表
          this.getCateList()
        })
        // 隐藏添加分类对话框
        this.showAddDialogVisiable = false
      },
      // 添加分类对话框关闭
      addFormDialogClosed() {
        // 表单清空
        this.$refs.addCateFormRef.resetFields()
        // 级联选择器数据清空
        this.selectedKeys = []
        this.addCateForm.cat_id = ''
        this.addCateForm.cat_level = ''
      }
    }
  }

</script>
<style lang="less" scoped>
  .el-card {
    margin-top: 15px;
  }

  .tree-table {
    margin-top: 20px;
  }

  .el-cascader {
    width: 100%;
  }

</style>
